package com.x.crm.dao;

import com.x.crm.model.Customer;
import com.x.crm.model.CustomerLevel;
import com.x.crm.model.Intention;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by x on 2017/11/13.
 */
@Repository
public class CustomerDao extends BaseDao {
    private static final String selectByIdSql = "SELECT * FROM CRM_CUSTOMER WHERE CUSTOMER_ID = ?";
    public Customer selectById(Customer customerWhere) throws Exception {
        StringBuilder sb = new StringBuilder(selectByIdSql);
        List<Object> params = new ArrayList<Object>();
        params.add(customerWhere.getCustomerId());
        List<Customer> customers = this.queryForList(sb.toString(), Customer.class, params);
        if(customers.size() == 1) {
            return customers.get(0);
        }
        return new Customer();
    }


    /**
     * 查询基本的客户信息
     * 201807版本
     */
    private static final String selectCustomerListSql = "SELECT * FROM CRM_CUSTOMER  T1 WHERE 1=1 ";
    public List<Customer> selectCustomerList(Customer customer) throws Exception {
        StringBuilder sql = new StringBuilder(selectCustomerListSql);
        List<Object> params = new ArrayList<Object>();
        searchCondition(customer,sql,params);
        return this.queryForList(sql.toString(), Customer.class, params, customer);
    }

    private static final String selectCustomerNumSql = "SELECT COUNT(T1.CUSTOMER_ID) FROM CRM_CUSTOMER  T1 WHERE 1=1 ";
    public int selectCustomerNum(Customer customer) throws Exception {
        StringBuilder sql = new StringBuilder(selectCustomerNumSql);
        List<Object> params = new ArrayList<Object>();
        searchCondition(customer, sql, params);
        return this.queryForInt(sql.toString(), params);
    }


    private void searchCondition(Customer customer, StringBuilder sql, List<Object> params) {
        if (this.isNotNull(customer.getCustomerOpenId())) {
            sql.append("AND T1.CUSTOMER_OPEN_ID = ? ");
            params.add(customer.getCustomerOpenId());
        }
        if (this.isNotNull(customer.getCustomerPhone())) {
            sql.append("AND T1.CUSTOMER_PHONE = ? ");
            params.add(customer.getCustomerPhone());
        }

    }

//    private static final String baseOwnerCustomerQuerySql = "SELECT  T1.*  FROM  CRM_CUSTOMER  T1 , CRM_INTENTION T2 WHERE T1.CUSTOMER_ID = T2.CUSTOMER_ID AND 1=1 ";
//    public List<Customer> selectOwnerCustomerList(Customer customer, Intention intention) throws Exception {
//        StringBuilder sql = new StringBuilder(baseOwnerCustomerQuerySql);
//        List<Object> params = new ArrayList<Object>();
////        if (this.isNotNull(intention.getRechargerId())) {
////            sql.append("AND (T2.RECHARGER_ID = ? or T2.FOLLOWER_ID= ?)");
////            params.add(intention.getRechargerId());
////            params.add(intention.getRechargerId());
////        }
//        if (this.isNotNull(intention.getIntentionState())) {
//            sql.append("AND (T2.INTENTION_STATE = ?)");
//            params.add(intention.getIntentionState());
//        }
//        return this.queryForList(sql.toString(), Customer.class, params, customer);
//    }
}
